package com.longdaji.decoration.ui.activitiesOfMine.pendingReceiving;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.longdaji.decoration.Constants;
import com.longdaji.decoration.R;
import com.longdaji.decoration.adapter.PendingReceivingOrderAdapter;
import com.longdaji.decoration.base.BaseActivity;
import com.longdaji.decoration.bean.Orders;
import com.longdaji.decoration.utils.LogUtil;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class PendingReceivingActivity extends BaseActivity implements PendingReceivingContract.View {

    @Inject
    PendingReceivingContract.Presenter mPresenter;
    @BindView(R.id.tv_tool_title)
    TextView tvTitle;
    @BindView(R.id.rv_pending_receiving)
    RecyclerView rvPendingReceiving;
    @BindView(R.id.sr_pending_receiving)
    SwipeRefreshLayout srPendingReceiving;

    private List<Orders.OrdersBean.OrderBean> mOrderList = new ArrayList<>();
    private PendingReceivingOrderAdapter mAdapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pending_receiving);
        setUnBinder(ButterKnife.bind(this));
        if (mPresenter != null)
            mPresenter.setView(this);
        tvTitle.setText("待收货");

        initView();


    }

    private void initView() {
        rvPendingReceiving.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new PendingReceivingOrderAdapter(mOrderList);
        mAdapter.setConfirmOrderListener(new PendingReceivingOrderAdapter.ConfirmOrderListener() {
            @Override
            public void onConfirmOrderClick(int position) {
                mPresenter.onConfirmOrderClick(mOrderList.get(position).getOrderNo());
            }
        });
        mAdapter.setSeeLogisticsListener(new PendingReceivingOrderAdapter.SeeLogisticsListener() {
            @Override
            public void onSeeLogisticsClick(int position) {
//                mPresenter.onSeeLogisticsClick(mOrderList.get(position).getOrderNo());
                Intent intent = new Intent(PendingReceivingActivity.this, SeeLogisticsActivity.class);
                intent.putExtra(Constants.ORDER_NO, mOrderList.get(position).getOrderNo());
                startActivity(intent);
            }
        });
        rvPendingReceiving.setAdapter(mAdapter);
        srPendingReceiving.setColorSchemeColors(getResources().getColor(R.color.colorPrimary));
        srPendingReceiving.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                refreshData();
            }
        });
        mPresenter.initData();

    }

    private void refreshData() {
        mPresenter.initData();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mPresenter != null)
            mPresenter.detachView();
    }

    @OnClick({R.id.rl_tool_back})
    void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rl_tool_back:
                finish();
                break;
            default:
                break;
        }
    }

    @Override
    public void showPendingReceivingOrders(List<Orders.OrdersBean.OrderBean> pendingReceivingOrderList) {
        Collections.reverse(pendingReceivingOrderList);
        if (pendingReceivingOrderList.size() > 0) {
            mAdapter.setData(pendingReceivingOrderList);
        } else {
            Toast.makeText(this, "无待收货款订单", Toast.LENGTH_SHORT).show();
            mAdapter.clearData();
        }
        if (srPendingReceiving != null && srPendingReceiving.isRefreshing()) {
            srPendingReceiving.setRefreshing(false);
        }
    }
}
